<?php
/*
 * glbpay_application_top.php
 *
 */
  define('IS_ADMIN_FLAG', false);

  error_reporting(0);//禁用错误报告
  $show_all_errors = true;
  $current_page_base = 'glbpayipn';

  define('PAGE_PARSE_START_TIME', microtime());
  error_reporting(E_ALL & ~E_NOTICE);

  @ini_set("arg_separator.output","&");

  //引用文件
  if (file_exists('includes/local/configure.php')) {
    include(DIR_WS_INCLUDES . '/local/configure.php');
  }
  if (file_exists('includes/configure.php')) {
    include('includes/configure.php');
  }
 
  require('includes/classes/class.base.php');
  require('includes/classes/class.notifier.php');
  require(DIR_WS_INCLUDES . 'filenames.php');
  
  $zco_notifier = new notifier();

  //建立数据库连接
  require('includes/classes/db/' .DB_TYPE . '/query_factory.php');
  $db = new queryFactory();
  if ( !$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE, USE_PCONNECT, false) ) {
    die('Cannot connect to database. Please notify webmaster.');
    exit;
  }

  //设置请求类型，ssl或nossl
  $request_type = (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1' || strstr(strtoupper($_SERVER['HTTP_X_FORWARDED_BY']),'SSL') || strstr(strtoupper($_SERVER['HTTP_X_FORWARDED_HOST']),'SSL'))  ? 'SSL' : 'NONSSL';

  require(DIR_WS_INCLUDES . 'database_tables.php'); 
  require(DIR_WS_CLASSES . 'cache.php');     
  $zc_cache = new cache;

  if ($za_dir = @dir(DIR_WS_INCLUDES . 'extra_datafiles')) {
    while ($zv_file = $za_dir->read()) {
      if (preg_match('/\.php$/', $zv_file) > 0) {
        require(DIR_WS_INCLUDES . 'extra_datafiles/' . $zv_file);
      }
    }
    $za_dir->close();
  }
//从数据库中获取配置，并设置为常量，允许缓存，缓存时间是150
  $configuration = $db->Execute('select configuration_key , configuration_value
                                 from ' . TABLE_CONFIGURATION, '', true, 150);
  while (!$configuration->EOF) {
    define($configuration->fields['configuration_key'], $configuration->fields['configuration_value']);
    $configuration->MoveNext();
  } 

  require(DIR_WS_FUNCTIONS . 'functions_general.php');
  require(DIR_WS_FUNCTIONS . 'html_output.php');
  
  //设置顶级域名
  $http_domain = zen_get_top_level_domain(HTTP_SERVER);//获得地址的顶级域名
  $https_domain = zen_get_top_level_domain(HTTPS_SERVER);
  $current_domain = (($request_type == 'NONSSL') ? $http_domain : $https_domain);
  if (SESSION_USE_FQDN == 'False') $current_domain = '.' . $current_domain;
  
  require(DIR_WS_CLASSES . 'shopping_cart.php');
  require(DIR_WS_CLASSES . 'navigation_history.php');
  require(DIR_WS_FUNCTIONS . 'sessions.php');
  
  require(DIR_WS_CLASSES . 'template_func.php');
  require(DIR_WS_CLASSES . 'message_stack.php');
  $template = new template_func(DIR_WS_TEMPLATE);
  zen_session_name('zenid');
  zen_session_save_path(SESSION_WRITE_DIRECTORY);
  session_set_cookie_params(0, '/', (zen_not_null($current_domain) ? $current_domain : ''));
  
 if (SESSION_FORCE_COOKIE_USE == 'True') {
    zen_setcookie('cookie_test', 'please_accept_for_session', time()+60*60*24*30, '/', (zen_not_null($current_domain) ? $current_domain : ''));
    if (isset($_COOKIE['cookie_test'])) {
       zen_session_start();
       $session_started = true;
    }
  } elseif (SESSION_BLOCK_SPIDERS == 'True') {
       $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
       $spider_flag = false;

       if (zen_not_null($user_agent)) {
          $spiders = file(DIR_WS_INCLUDES . 'spiders.txt');

       for ($i=0, $n=sizeof($spiders); $i<$n; $i++) {
            if (zen_not_null($spiders[$i])) {
                if (is_integer(strpos($user_agent, trim($spiders[$i])))) {
                   $spider_flag = true;
				   break;
                  }
            }
        }
      }
      if ($spider_flag == false) {
         zen_session_start();
         $session_started = true;
      }
  } else {
    zen_session_start();
    $session_started = true;
  }

  //将存储在数据库中的session值取出并解码
  if (!$_SESSION['customer_id']) {
    $sql = "SELECT * FROM " . TABLE_GLBPAY_SESSION . " WHERE orders_id = '" . $order_id . "' order by unique_id desc limit 1";
    $stored_session = $db->Execute($sql);
    $_SESSION = unserialize(base64_decode($stored_session->fields['session_info']));
  }

  if (!$_SESSION['cart']) {
    $_SESSION['cart'] = new shoppingCart;
  }

  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();  
  
  if (!$_SESSION['language'] || isset($_GET['language'])) {

    require(DIR_WS_CLASSES . 'language.php');
    $lng = new language();
    if (isset($_GET['language']) && zen_not_null($_GET['language'])) {
      $lng->set_language($_GET['language']);
    } else {
      $lng->get_browser_language();
      $lng->set_language(DEFAULT_LANGUAGE);
    }
    $_SESSION['language'] = $lng->language['directory'];
    $_SESSION['languages_id'] = $lng->language['id'];
  }
  
  require('includes/languages/'.$_SESSION['language'] . '/modules/payment/glbpay.php');
  
 
  if (!$_SESSION['customer_id']) {
    $sql = "SELECT * FROM " . TABLE_GLBPAY_SESSION . " WHERE orders_id = '" . $order_id . "' order by unique_id desc limit 1";
    $stored_session = $db->Execute($sql);
    $_SESSION = unserialize(base64_decode($stored_session->fields['session_info']));
  }

  if (!$_SESSION['cart']) {
    $_SESSION['cart'] = new shoppingCart;
  }

?>
